/**
 * @project heima-leadnews
 * @author MECHREVO
 * @date 2024/1/21 19:51:03
 * @version 1.0
 */
package com.heima.wemedia.service.impl;

import com.heima.fegin.schedule.IScheduleClient;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dto.TaskDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.service.WmAutoScanService;
import com.heima.wemedia.service.WmTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class WmTaskServiceImpl implements WmTaskService {
    @Autowired
    private IScheduleClient iScheduleClient;
    @Autowired
    private WmAutoScanService wmAutoScanService;
    @Override
    @Async
    public void addTask(WmNews wmNews) {
        //封装数据
        TaskDto taskDto = new TaskDto();
        taskDto.setTaskType(1001);
        taskDto.setPriority(1);
        taskDto.setParameters(String.valueOf(wmNews.getId()).getBytes());
        taskDto.setExecuteTime(wmNews.getPublishTime().getTime());

        //远程调用schedule微服务，添加任务
        iScheduleClient.addTask(taskDto);
    }

    /**
     * 定时拉取审核文章任务，进行文章审核操作
     * 执行频次：每秒执行一次
     */
    @Scheduled(cron = "0/1 * * * * ?")
    public void pullTask() {
        ResponseResult result = iScheduleClient.pullTask(1001, 1);
        Integer code = result.getCode();
        Object data = result.getData();
        if(code==200){
            log.info("========定时拉取审核文章任务执行了，本次处理的文章id:{}=========:",data);
            Integer newsId = Integer.valueOf(data.toString());
            wmAutoScanService.autoScan(newsId);
        }
    }
}
